Main page

Row

Distribution of order volume placed during each hour of day

Row

Top 10 best-selling items by day of the week

Pie chart

Row

Pie chart of department by sales volume

---
title: "Homework 4"
author: "Anusorn Thanataveerat"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
library(ggplot2)
library(plotly)
library(tidyverse)
library(flexdashboard)
library(p8105.datasets)

# Make some noisily increasing data
data("instacart")
set.seed(1)
#Randomly sample 1% from the original observations
instacart_subset <- instacart %>% sample_frac(0.01)

```

Main page
=======================================================================

Row
-----------------------------------------------------------------------

### Distribution of order volume placed during each hour of day

```{r}
#set up plot element
f <- list(
  family = "Courier New, monospace",
  size = 18,
  color = "#7f7f7f"
)
x <- list(
  title = "Hour of the day",
  titlefont = f
)
#####
instacart_subset %>% 
  group_by(order_id) %>% 
  count(order_hour_of_day) %>%
  plot_ly(x = ~order_hour_of_day, type = "histogram", alpha = 0.5) %>% 
  layout(xaxis = x)
```

Row
-----------------------------------------------------------------------

### Top 10 best-selling items by day of the week 

```{r}
top10_product <- instacart_subset %>% count(product_name, sort = TRUE) %>% 
  top_n(10) %>% select(product_name)

inner_join(instacart_subset, top10_product) %>% 
   mutate(order_dow = fct_recode(as.factor(order_dow), 
   Sunday = '0', Monday = '1', Tuesday = '2', Wednesday = '3',
   Thursday = '4', Friday = '5', Saturday = '6')) %>% 
   group_by(order_dow) %>% 
   count(product_name, sort = TRUE) %>% 
   spread(key = order_dow, value = n) %>% 
   plot_ly(x = ~Sunday, y = ~product_name, name = 'Sunday', type = 'scatter',
           mode = 'markers', marker = list(color = 'red')) %>% 
   add_trace(x = ~Monday, y = ~product_name, name = 'Monday', type = 'scatter',
           mode = 'markers', marker = list(color = 'yellow')) %>% 
   add_trace(x = ~Tuesday, y = ~product_name, name = 'Tuesday', type = 'scatter',
           mode = 'markers', marker = list(color = 'pink')) %>% 
   add_trace(x = ~Wednesday, y = ~product_name, name = 'Wednesday', type = 'scatter',
           mode = 'markers', marker = list(color = 'green')) %>% 
   add_trace(x = ~Thursday, y = ~product_name, name = 'Thursday', type = 'scatter',
           mode = 'markers', marker = list(color = 'orange')) %>% 
   add_trace(x = ~Friday, y = ~product_name, name = 'Friday', type = 'scatter',
           mode = 'markers', marker = list(color = 'blue')) %>% 
   add_trace(x = ~Saturday, y = ~product_name, name = 'Saturday', type = 'scatter',
           mode = 'markers', marker = list(color = 'violet')) %>% 
   layout(title = 'Tope 10 best-selling prodcut',
          xaxis = list(title = ''))
```


Pie chart
=======================================================================

Row
-----------------------------------------------------------------------
### Pie chart of department by sales volume

```{r}
instacart_subset %>% 
  group_by(department) %>% 
  count() %>% 
  plot_ly(labels = ~department, values = ~n, type = 'pie',
        textposition = 'inside',
        textinfo = 'label+percent',
        insidetextfont = list(color = '#FFFFFF'),
        hoverinfo = 'text',
        text = ~paste(department, '\n', n, ' items'),
        marker = list(colors = colors,
        line = list(color = '#FFFFFF', width = 1)),
        showlegend = FALSE) %>%
  layout(title = 'Pie chart of department by sales volume',
        xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
        yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))

```